/*****************
 * rewrite of as2 class in as3
 * as2 author ors
 * author marcel
 */

package ch.sfug.net.loader {
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	import ch.sfug.net.loader.AbstractLoader;		

	public class MediaLoader extends AbstractLoader {

		private var loader:Loader;
		private var lc:LoaderContext;

		public function MediaLoader( url:String = null ) {
			super(new URLRequest(url));
			loader = new Loader();
		}

		
		/**
		 * loads an swf or image file from the specified url. if you specified it already in the constructor you can omit it.
		 */
		override public function load( request:URLRequest = null ):void {

			this.request = request;

			loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, delegate);
			loader.contentLoaderInfo.addEventListener(Event.INIT, onLoadEnds);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, delegate);
			loader.contentLoaderInfo.addEventListener(Event.OPEN, delegate);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, delegate);
			loader.contentLoaderInfo.addEventListener(Event.UNLOAD, delegate);

			super.onLoadStarts();
			loader.load( this.request, this.lc );
		}

		
		/**
		 * unloads the loaded data
		 */
		public function unload():void {
			
			loader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, delegate);
			loader.contentLoaderInfo.removeEventListener(Event.INIT, onLoadEnds);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, delegate);
			loader.contentLoaderInfo.removeEventListener(Event.OPEN, delegate);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, delegate);
			loader.contentLoaderInfo.removeEventListener(Event.UNLOAD, delegate);
            
			stop();
            
			try {
				
				// remove bitmap from memory
				var li:LoaderInfo = loader.contentLoaderInfo;
				if( li.childAllowsParent && li.content is Bitmap ) {
					(li.content as Bitmap).bitmapData.dispose();
				}
				loader.unload();
			} catch( e:Error ) {
			}
		}

		
		/**
		 * stops the download of the cssfile
		 */
		public override function stop(  ):void {
			try {
				loader.close();
			} catch( e:Error ) { 
			}
			loader.unload();
		}

		
		/**
		 * returns the loader instance that has loaded the swf/image from the given url
		 */
		public function getResult():Loader {
			return loader;
		}
		
		
		public function set context( lc:LoaderContext ):void {
			this.lc = lc;
		}

		
		/**
		 * simply delegates the event
		 */
		private function delegate( event:Event ):void {
			dispatchEvent(event);
		}
		
		/**
		 * should be called when the load is finished
		 */
		protected override function onLoadEnds( e:Event = null ):void {
			super.onLoadEnds();
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
	}
}